package controller;

import java.io.IOException;
import java.sql.SQLException;
import java.sql.Timestamp;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;

import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import manager.LeaveManager;
import manager.UserManager;
import data.dao.LeaveDAOException;
import data.dao.UserDAOException;
import data.dao.daoImplementation.NotFoundException;
import data.dto.HolidaysDTO;
import data.dto.UserDTO;
import data.searchdto.UserSearchDTO;

/**
 * Servlet implementation class AdminPublicHoliday
 */
@WebServlet("/AdminPublicHoliday")
public class AdminPublicHoliday extends HttpServlet {
	private static final long serialVersionUID = 1L;

	/**
	 * @see HttpServlet#HttpServlet()
	 */
	public AdminPublicHoliday() {
		super();
		// TODO Auto-generated constructor stub
	}

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse
	 *      response)
	 */
	protected void doGet(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		// TODO Auto-generated method stub
		try {
			doProcess(request, response);
		} catch (UserDAOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse
	 *      response)
	 */
	protected void doPost(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		// TODO Auto-generated method stub
		try {
			doProcess(request, response);
		} catch (UserDAOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}

	private void doProcess(HttpServletRequest request,
			HttpServletResponse response) throws UserDAOException,
			ServletException, IOException {
		String page = "";
		UserDTO user = util.CurrentUser.getCurrentUser(request.getSession());

		// check user
		if (user == null) {
			page = util.Constants.homePage;
		} else if (!util.CurrentUser.isAdmin(user)) {
			page = util.Constants.homePage;
		} else {

			String type = request.getParameter("type") == null ? "" : request
					.getParameter("type").trim();

			LeaveManager lm = new LeaveManager();

			if (type.equalsIgnoreCase("delete")) {
				// do delete process here
				String id = request.getParameter("id");
				try {
					try {
						lm.deleteHoliday(Integer.parseInt(id));
					} catch (NumberFormatException e) {
						// TODO Auto-generated catch block
						e.printStackTrace();
					} catch (LeaveDAOException e) {
						// TODO Auto-generated catch block
						e.printStackTrace();
					} catch (SQLException e) {
						// TODO Auto-generated catch block
						e.printStackTrace();
					}
				} catch (NotFoundException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
			} else if (type.equalsIgnoreCase("insert")) {
				SimpleDateFormat sdf = new SimpleDateFormat(
						"yyyy-MM-dd hh:mm:ss");
				java.util.Date date = null;
				try {
					date = sdf.parse(request.getParameter("holidayDate")
							+ " 00:00:00");
				} catch (ParseException e1) {
					// TODO Auto-generated catch block
					e1.printStackTrace();
				}
				java.sql.Timestamp timest = new java.sql.Timestamp(
						date.getTime());

				HolidaysDTO holiday = new HolidaysDTO();
				holiday.setHolidayName(request.getParameter("holidayName"));
				holiday.setHolidayDate(timest);
				holiday.setStatus((short) 1);
				try {
					lm.insertHoliday(holiday);
				} catch (LeaveDAOException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				} catch (SQLException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
				// request.setAttribute("holidays", lm.findAllHoliday());
			} else if (type.equalsIgnoreCase("update")) {
				SimpleDateFormat sdf = new SimpleDateFormat(
						"yyyy-MM-dd hh:mm:ss");
				java.util.Date date = null;
				try {
					date = sdf.parse(request.getParameter("holidayDate")
							+ " 00:00:00");
				} catch (ParseException e1) {
					// TODO Auto-generated catch block
					e1.printStackTrace();
				}
				java.sql.Timestamp timest = new java.sql.Timestamp(
						date.getTime());

				HolidaysDTO holiday = new HolidaysDTO();
				holiday.setId(Short.parseShort(request.getParameter("id")));
				holiday.setHolidayName(request.getParameter("holidayName"));
				holiday.setHolidayDate(timest);
				holiday.setStatus((short) 1);
				try {
					lm.updateHoliday(holiday);
				} catch (LeaveDAOException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				} catch (SQLException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				} catch (NotFoundException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
				// request.setAttribute("holidays", lm.updateHoliday(holiday));
			}

			try {
				request.setAttribute("holidays", lm.findAllHoliday());
			} catch (LeaveDAOException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			} catch (SQLException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
			page = "/admin/managePublicHoliday.jsp?show=All";
		}

		RequestDispatcher rd = request
				.getRequestDispatcher(page);
		rd.forward(request, response);
	}

}
